package it.netsphere.nswing.event;

/**
 * 
 * @author alberto.goldoni
 * 
 */

public final class NSEvent {

    private String evtCode;

    private Object param;

    private Object param2;

    private NSEventMode mode;

    private Exception exception;

    private NSEvent() {
    }

    public NSEvent(String evtCode, Object param) {
        this.evtCode = evtCode.toString();
        this.param = param;
        setMode(NSEventMode.REQUEST);
    }

    public String getEvtCode() {
        return evtCode;
    }

    public void setEvtCode(String evtCode) {
        this.evtCode = evtCode;
    }

    public Object getParam() {
        return param;
    }

    public void setParam(Object param) {
        this.param = param;
    }

    public Object getParam2() {
        return param2;
    }

    public void setParam2(Object param2) {
        this.param2 = param2;
    }

    public String toString() {
        return String.format("%s[code=%s,param=%s,mode=%s,error=%s]", getClass().getSimpleName(), evtCode, param, getMode(), getException() != null);
    }

    public NSEvent createReply(Object param) {
        NSEvent event = new NSEvent();

        event.setEvtCode(getEvtCode());
        event.setMode(NSEventMode.RESPONSE);
        event.setParam(param);

        return event;
    }

    public NSEvent createProgressBefore(Object param) {
        NSEvent event = new NSEvent();

        event.setEvtCode(getEvtCode());
        event.setMode(NSEventMode.PROGRESS_BEFORE);
        event.setParam(param);

        return event;
    }

    public NSEvent createProgressAfter(Object param) {
        NSEvent event = new NSEvent();

        event.setEvtCode(getEvtCode());
        event.setMode(NSEventMode.PROGRESS_AFTER);
        event.setParam(param);

        return event;
    }

    public NSEvent createError(Exception e) {
        NSEvent event = new NSEvent();

        event.setEvtCode(getEvtCode());
        event.setMode(NSEventMode.ERROR);
        event.setParam(e);
        event.setException(e);

        return event;
    }

    public NSEvent createErrorReply(Exception ex) {
        NSEvent event = new NSEvent();

        event.setEvtCode(getEvtCode());
        event.setMode(NSEventMode.RESPONSE);
        event.setException(ex);

        return event;
    }

    public NSEventMode getMode() {
        return mode;
    }

    public void setMode(NSEventMode mode) {
        this.mode = mode;
    }

    public Exception getException() {
        return exception;
    }

    public void setException(Exception exception) {
        this.exception = exception;
    }

    public boolean isError() {
        return exception != null;
    }
}
